package org.suspi.skarabee.monitoring;


import java.util.ArrayList;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.supsi.skarabee.model.NodeModel;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.merge.skarabee.R;


public class MonitorActivity extends FragmentActivity implements Observer{





	FragmentManager fragmentManager = getSupportFragmentManager();
	ChartFragment chartFragment;
	PortInfoFragment port1;
	PortInfoFragment port2;		
	PortInfoFragment port3;		
	PortInfoFragment port4;
	NodeFragment nodeFragment;
	NodeModel nodeModel;
	ArrayList<PortInfoFragment> portFragmentList = new ArrayList<PortInfoFragment>();
	MHandler handler;

	/* (non-Javadoc)
	 * @see android.app.Activity#onPostCreate(android.os.Bundle)
	 */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		setupView();
		startObserving();
	}

	private void startObserving() {
		nodeModel.addObserver(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.monitor_activity);
		this.nodeModel = NodeModel.getInstance();
		createFrags();
		addFragments();
		handler = new MHandler();
	}

	private void createFrags(){

		port1 = new PortInfoFragment();
		port2 = new PortInfoFragment();		
		port3 = new PortInfoFragment();		
		port4 = new PortInfoFragment();
		nodeFragment = new NodeFragment();
		portFragmentList.add(port1);
		portFragmentList.add(port2);
		portFragmentList.add(port3);
		portFragmentList.add(port4);
		chartFragment = new ChartFragment();
	}

	private void addFragments(){
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.add(R.id.p1_frame, port1);
		fragmentTransaction.add(R.id.p2_frame, port2);
		fragmentTransaction.add(R.id.p3_frame, port3);
		fragmentTransaction.add(R.id.p4_frame, port4);
		fragmentTransaction.add(R.id.rightLayout, chartFragment);
		fragmentTransaction.add(R.id.nodeFrame, nodeFragment);
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
		getSupportFragmentManager().executePendingTransactions();
	}

	private void setupView(){
		chartFragment.startChart();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onOptionsClick(View view){
		Toast toast = Toast.makeText(getApplicationContext(), "CLIIIICK", Toast.LENGTH_SHORT);
		toast.show();
	}

	@Override
	public void update(Observable observable, Object data) {
		Bundle nodeInfo = nodeModel.getNodeInfos();
		notifyMessage(nodeInfo);
		nodeFragment.setData(nodeInfo);
		chartFragment.addDatas(nodeModel.getChartInfos());
		for(int i = 1; i <5; i++){
			portFragmentList.get(i-1).setData(nodeModel.getPortInfos(i));
		}
	}

	private class MHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			if(bundle.containsKey("id")) {
				String value = bundle.getString("id");
				TextView tv = (TextView)findViewById(R.id.nodeIdTV);
				tv.setText("Node ID: " + value);
			}
			if(bundle.containsKey("config")){
				String value = bundle.getString("config");
				//TextView tv = (TextView)findViewById(R.id.configTV);
				//tv.setText(value);
			}
			if(bundle.containsKey("status")){
				String value = bundle.getString("status");
				//TextView tv = (TextView)findViewById(R.id.statusTV);
				//tv.setText(value);
			}
		}
	}

	private void notifyMessage(Bundle b){
		Message msg = handler.obtainMessage();
		msg.setData(b);
		handler.sendMessage(msg);
	}
	
}	
